class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        if(piles.length > h)return -1;
        int left = 1, right = 0;
        for(int pile : piles){
            right = Math.max(pile, right);
        }
        while(left<right){
            int mid = (left+right)>>1;
            if(check(piles, h, mid)){
                right = mid;
            }else{
                left = mid+1;
            }
        }
        return right;
    }

    private boolean check(int[] piles, int h, int n){
        int x = 0;//元素个数
        for(int pile : piles){
            x += pile%n==0?pile/n:pile/n+1;
        }
        return x <= h;
    }
}
/**
    可以看做将数组piles拆分成h个元素，求拆分后最大元素中的最小值。
 */